package com.rrd.dw.mr.gzip.kafka;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import com.rrd.dw.utils.DateUtil;
import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.alibaba2.AliPayOrder;
import com.ucredit.babel.common.model.sms.SmsAppInfo;
import com.ucredit.babel.common.model.sms.SmsContentInfo;
import com.ucredit.babel.common.model.sms.SmsDeviceInfo;
import com.ucredit.babel.common.model.sms.SmsReportRecord;
import com.ucredit.babel.common.model.sms.SmsUserInfo;
/**
 * 解析短信信息
 * @author liubaoxin
 *
 */

@Deprecated
public class SmsReportParse {
	public static final String CRT001 =  "\001";
	public static final Pattern CRT001_PATTERN =  Pattern.compile("\001");
	private  Text val = new Text();
	private  Text key= new Text();
	private  String user_id="";
	private String timestamstr="";
	private String job_id ="";
	private String system_user_key ="";
	private String idno="";
	private StringBuilder sb = new StringBuilder(500);
	/**
	 * 解析用户短信基础数据
	 * @param call
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parseSmsReportBase(Context context,SmsReportRecord smsRecord,String successful) throws IOException, InterruptedException{
		
		StringBuilder sb = new StringBuilder(200);
    	Job job = smsRecord.getJob();
    	SmsAppInfo appInfo = smsRecord.getSmsAppInfo();
    	SmsUserInfo userInfo= smsRecord.getSmsUserInfo();
    	SmsDeviceInfo deviceInfo = smsRecord.getSmsDeviceInfo();
    	
    	if(appInfo==null ){
    		appInfo = new SmsAppInfo();
    	}
    	if(userInfo==null ){
    		userInfo = new SmsUserInfo();
    	}
    	if(deviceInfo==null ){
    		deviceInfo = new SmsDeviceInfo();
    	}
    	 sb.delete(0,sb.length());
    	 this.timestamstr=DateUtil.formateDateStr(new Date(smsRecord.getTimestamp()));
    	 this.job_id = job.getJobID()==null?"":job.getJobID();
    	 this.system_user_key = userInfo.getSystem_unique_key()==null?"":userInfo.getSystem_unique_key();
    	 this.idno = smsRecord.getIdNumber()==null?"":smsRecord.getIdNumber();
    	 this.user_id = MD5Util.getMd5(timestamstr+idno+this.job_id+this.system_user_key);
    	 sb.append(this.user_id).append(CRT001);
    	 sb.append(this.idno).append(CRT001);
    	 sb.append(timestamstr).append(CRT001);
         sb.append(this.system_user_key).append(CRT001);
         sb.append(job_id).append(CRT001);
 

    	//手机号
    	sb.append(userInfo.getPhone()==null?"":userInfo.getPhone()).append(CRT001);
    	//应用唯一标识
    	sb.append(appInfo.getApp_bundle()==null?"":appInfo.getApp_bundle()).append(CRT001);
    	//应用版本
    	sb.append(appInfo.getApp_version()==null?"":appInfo.getApp_version()).append(CRT001);
    	//是否越狱
    	sb.append(deviceInfo.getJailbroken()==null?"":deviceInfo.getJailbroken()).append(CRT001);
    	//屏幕尺寸
    	sb.append(deviceInfo.getScreen()==null?"":deviceInfo.getScreen()).append(CRT001);
    	//模拟器
    	sb.append(deviceInfo.getSimulator()==null?"":deviceInfo.getSimulator()).append(CRT001);
    	//网络类型
    	sb.append(deviceInfo.getNetwork_type()==null?"":deviceInfo.getNetwork_type()).append(CRT001);
    	//操作系统
    	sb.append(deviceInfo.getOs()==null?"":deviceInfo.getOs()).append(CRT001);
    	//是否wifi
    	sb.append(deviceInfo.getWifi()==null?"":deviceInfo.getWifi()).append(CRT001);
    	//设备号
    	sb.append(deviceInfo.getImei()==null?"":deviceInfo.getImei()).append(CRT001);
    	//设备名
    	sb.append(deviceInfo.getDevice_name()==null?"":deviceInfo.getDevice_name()).append(CRT001);
    	//系统语言
    	sb.append(deviceInfo.getLanguage()==null?"":deviceInfo.getLanguage()).append(CRT001);
    	//系统版本
    	sb.append(deviceInfo.getOs_version()==null?"":deviceInfo.getOs_version()).append(CRT001);
    	//厂商
    	sb.append(deviceInfo.getModel()==null?"":deviceInfo.getModel()).append(CRT001);
    	//运营商
    	sb.append(deviceInfo.getCarrier()==null?"":deviceInfo.getCarrier());
    	
    	
    	key.set("smsReportBase");
    	val.set(sb.toString().replaceAll("[\\\\n\n\r\\\\r]", "|"));
    	context.write(key, val);
    	parseSmsReportContent(context,smsRecord);
    	 
	}
	
 
	/**
	 * 用户短信内容数据
	 * @param smsRecord
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	private   void parseSmsReportContent(Context context, SmsReportRecord smsRecord) throws IOException, InterruptedException{
		
    	StringBuilder sb = new StringBuilder(200);
    	List<SmsContentInfo> list = smsRecord.getSmsContentInfo();
    	
    	String s="";
    	
    	for(SmsContentInfo o :list){
    		 
    		sb.delete(0, sb.length());
    		sb.append(this.user_id).append(CRT001).append(this.idno).append(CRT001);
    		//时间戮
    		sb.append(this.timestamstr).append(CRT001);
 
    		//短信主键编号
    		sb.append(o.get_id()==null?"":o.get_id()).append(CRT001);
    		//会话的id
    		sb.append(o.getThread_id()==null?"":o.getThread_id()).append(CRT001);
    		//接收人电话
    		sb.append(o.getAddress()==null?"":o.getAddress()).append(CRT001);
    		//接收人姓名
    		sb.append(o.getPerson()==null?"":o.getPerson()).append(CRT001);
    		//发收时间
    		sb.append(o.getDate()==null?"":o.getDate()).append(CRT001);
    		//协议
    		sb.append(o.getProtocol()==null?"":o.getProtocol()).append(CRT001);
    		//短信类型
    		sb.append(o.getType()==null?"":o.getType()).append(CRT001);
    		//是否阅读
    		sb.append(o.getRead()==null?"":o.getRead()).append(CRT001);
    		//发出的短信状态
    		sb.append(o.getStatus()==null?"":o.getStatus()).append(CRT001);
    		//短信主题
    		sb.append(o.getSubject()==null?"":o.getSubject()).append(CRT001);
    		//短信内容
    		sb.append(o.getBody()==null?"":o.getBody());
    		
    		s=sb.toString().replaceAll("[\\\\n\n\r\\\\r]", "|");
    		s=MD5Util.getMd5(s)+CRT001+s;
    		key.set("smsReportContent");
        	val.set(s);
        	context.write(key, val);
    	}
    	
	}
}
